package com.dupt.common.dingtalk;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiV2DepartmentGetRequest;
import com.dingtalk.api.request.OapiV2UserGetRequest;
import com.dingtalk.api.request.OapiV2UserGetbymobileRequest;
import com.dingtalk.api.response.OapiV2DepartmentGetResponse;
import com.dingtalk.api.response.OapiV2UserGetResponse;
import com.dingtalk.api.response.OapiV2UserGetbymobileResponse;

import java.util.ArrayList;
import java.util.List;

/**
 * 获取钉钉用户信息
 *
 * @author zhuanyu.sun
 * @date 2025-06-11
 */
public class GainUserInfo {
    //根据手机号，获取用户ID
    private static String getUserIdByPhone(String phone,String accessToken){
        try {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getbymobile");
            OapiV2UserGetbymobileRequest req = new OapiV2UserGetbymobileRequest();
            req.setMobile(phone);
            OapiV2UserGetbymobileResponse rsp = client.execute(req,accessToken );
            return rsp.getResult().getUserid();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static List<String> getUserInfoByPhoneNo(String phoneNo){
        try {
            String accessToken = AccessToken.getAccessToken();
            String userId = getUserIdByPhone(phoneNo, accessToken);
            if(userId!=null){
                DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
                OapiV2UserGetRequest req = new OapiV2UserGetRequest();
                req.setUserid(userId);
                OapiV2UserGetResponse rsp = client.execute(req, accessToken);
                List<String> list = new ArrayList<>();
                list.add(rsp.getResult().getUserid());//用户ID
                list.add(rsp.getResult().getName());//用户名
                list.add(rsp.getResult().getJobNumber());//工号
                Long parentId = getDepartInfo(rsp.getResult().getDeptIdList().get(0), accessToken).getResult().getParentId();
                list.add(parentId.toString());//部门ID
                list.add(getDepartInfo(parentId,accessToken).getResult().getName());//部门名称
                list.add(rsp.getResult().getTitle());//职位
                list.add(rsp.getResult().getAvatar());//头像
                return list;
            }else{
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //获取部门信息
    private static OapiV2DepartmentGetResponse getDepartInfo(Long departId,String accessToken){
        try {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/get");
            OapiV2DepartmentGetRequest req = new OapiV2DepartmentGetRequest();
            req.setDeptId(departId);
            OapiV2DepartmentGetResponse rsp = client.execute(req, accessToken);
            return rsp;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
